package models

import (
	"github.com/astaxie/beego/orm"
	_ "github.com/go-sql-driver/mysql"
	"fmt"
	"strconv"
)

//设备结构体 存放标签信息
type LabelInfo struct {
	Id                int64
	Labelid           string
	Labelip           string
	Labelport         string
	Labeltype         string
	Labeluser         string
	Position          string
	Registered        string
	Unit              string
	Department        string
	Coordinate        string
	Online            string
	Electricquantity  string
	Timestamp         string
	Changetime        string
	Alarmflag         string
	Badhandstatus     string
	Opencamera        string
	Wearingstatus     string
	Nearelectricity   string
	Networkstatus     string
}




//添加设备
func AddDevice(label_info *LabelInfo)(int64,error){
	db := orm.NewOrm()
	id,err := db.Insert(label_info)
	return id,err
}



//加载所有设备
func ReadLabel(labelDataChan chan string,username string,company string){
     fmt.Println(username)
	//使用原生查询
	var labelrows []LabelInfo
	o := orm.NewOrm()
	if _, err := o.Raw("SELECT * FROM label_info where unit = ? ",company).QueryRows(&labelrows); err != nil{
		fmt.Println("err")
		return
	}
	//fmt.Println("n =",n)
	var  i int64= 1;
	for _,labelrow := range labelrows{
		id := strconv.FormatInt(i,10)
		LabelInfos :=id+ "and"+labelrow.Labelid+
			"and"+labelrow.Labeluser+
			"and"+labelrow.Department+
			"and"+labelrow.Electricquantity+
			"and"+labelrow.Online+
			"and"+labelrow.Networkstatus+
			"and"+labelrow.Badhandstatus+
			"and"+labelrow.Wearingstatus
		labelDataChan <- LabelInfos
		i++
	}
}




















